<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Future 与 CompletableFuture</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-block pre {
            margin: 0;
            padding: 60px 24px 24px;
            overflow-x: auto;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .comparison-table {
            overflow: hidden;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .comparison-table th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: 600;
            padding: 16px;
            text-align: left;
        }
        
        .comparison-table td {
            padding: 16px;
            border-bottom: 1px solid #e5e7eb;
        }
        
        .comparison-table tr:last-child td {
            border-bottom: none;
        }
        
        .comparison-table tr:hover {
            background: #f9fafb;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            margin-bottom: 2rem;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl font-bold mb-6">深入理解 Future 与 CompletableFuture</h1>
                <p class="text-xl opacity-90 leading-relaxed">
                    探索 Java 并发编程中的异步任务处理艺术，从基础的 Future 到强大的 CompletableFuture，
                    掌握现代异步编程的核心技术。
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-clock mr-2"></i>阅读时间：10分钟
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-code mr-2"></i>Java 并发编程
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                在Java并发编程中，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Future</code>和
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">CompletableFuture</code>
                是两个非常重要的工具类。它们都用于处理异步任务，即在执行某个任务时，主线程可以不阻塞等待结果，
                从而提高程序的性能和响应性。然而，它们的使用场景和功能差异却很大。
            </p>
        </div>

        <!-- Concept Visualization -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">概念关系图</h2>
            <div class="mermaid">
                graph TD
                    A[异步编程] --> B[Future]
                    A --> C[CompletableFuture]
                    B --> D[基础功能]
                    B --> E[阻塞获取结果]
                    B --> F[任务状态检查]
                    C --> G[高级功能]
                    C --> H[链式操作]
                    C --> I[异常处理]
                    C --> J[任务组合]
                    C --> K[回调机制]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Future Section -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-hourglass-half text-purple-600 mr-3"></i>什么是 Future？
            </h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Future</code>
                是Java中提供的一种用于异步任务执行的接口。通过Future，我们可以在后台线程中执行一个耗时的操作，
                而主线程不需要等待该操作完成，可以继续执行其他任务。Future提供了一些方法来检查任务的执行状态和获取任务结果。
            </p>

            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="feature-icon bg-purple-200 text-purple-700">
                        <i class="fas fa-play-circle"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">get() 方法</h3>
                    <p class="text-gray-700">用于获取异步任务的执行结果。如果任务尚未完成，调用get()方法会阻塞当前线程，直到任务完成并返回结果。</p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <div class="feature-icon bg-blue-200 text-blue-700">
                        <i class="fas fa-check-circle"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">isDone() 方法</h3>
                    <p class="text-gray-700">用于检查任务是否已经完成，返回true表示任务已经完成，无论是正常完成还是异常完成。</p>
                </div>
                
                <div class="bg-gradient-to-br from-green-50 to-teal-50 p-6 rounded-xl">
                    <div class="feature-icon bg-green-200 text-green-700">
                        <i class="fas fa-times-circle"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">cancel() 方法</h3>
                    <p class="text-gray-700">用于取消任务。如果任务尚未开始执行，调用cancel()方法会使任务被取消；如果任务已经开始执行，则无法取消。</p>
                </div>
                
                <div class="bg-gradient-to-br from-yellow-50 to-orange-50 p-6 rounded-xl">
                    <div class="feature-icon bg-yellow-200 text-yellow-700">
                        <i class="fas fa-question-circle"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">isCancelled() 方法</h3>
                    <p class="text-gray-700">用于检查任务是否已被取消。</p>
                </div>
            </div>

            <div class="code-block">
                <pre><code class="language-java">import java.util.concurrent.*;

public class FutureDemo{
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        Callable&lt;Integer&gt; task = () -&gt; {
            Thread.sleep(2000); // 模拟耗时操作
            return 42;
        };

        Future&lt;Integer&gt; future = executorService.submit(task);
        System.out.println("主线程继续执行...");
        System.out.println("任务结果: " + future.get()); // 会阻塞，直到任务完成
        executorService.shutdown();
    }
}</code></pre>
            </div>

            <div class="highlight-box mt-6">
                <h3 class="text-xl font-semibold mb-3">
                    <i class="fas fa-exclamation-triangle mr-2"></i>Future 的不足
                </h3>
                <ul class="space-y-2">
                    <li><i class="fas fa-ban mr-2"></i><strong>阻塞调用：</strong>get()方法是阻塞的，意味着主线程会在调用get()时等待异步任务完成。</li>
                    <li><i class="fas fa-ban mr-2"></i><strong>缺少处理回调：</strong>Future本身不提供处理任务完成后的回调机制，需要通过轮询的方式判断任务是否完成。</li>
                </ul>
            </div>
        </div>

        <!-- CompletableFuture Section -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-rocket text-blue-600 mr-3"></i>什么是 CompletableFuture？
            </h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                <code class="bg-blue-100 text-blue-800 px-2 py-1 rounded">CompletableFuture</code>
                是Java 8引入的一个类，扩展了Future接口，提供了更强大的异步编程支持。它不仅能处理异步任务，
                还支持任务完成后的回调处理（如成功、失败等）。与Future相比，CompletableFuture更灵活，
                支持链式操作、多个任务的组合、异常处理等高级功能。
            </p>

            <div class="grid md:grid-cols-3 gap-4 mb-8">
                <div class="bg-gradient-to-r from-blue-500 to-purple-600 text-white p-6 rounded-xl">
                    <i class="fas fa-link text-3xl mb-3"></i>
                    <h3 class="text-xl font-semibold">链式操作</h3>
                    <p class="mt-2">支持流畅的链式调用，让异步编程更加优雅</p>
                </div>
                
                <div class="bg-gradient-to-r from-purple-600 to-pink-600 text-white p-6 rounded-xl">
                    <i class="fas fa-layer-group text-3xl mb-3"></i>
                    <h3 class="text-xl font-semibold">任务组合</h3>
                    <p class="mt-2">轻松组合多个异步任务，实现复杂的业务